package com.shuda.common.utils;

import org.springframework.beans.BeanUtils;

/**
 * Bean复制工具类
 * @author ink
 * @Description: Bean属性复制工具
 * @date 2024-10-10 14:30:00
 */
public class BeanCopyUtils {

    /**
     * 复制属性
     * @param source 源对象
     * @param target 目标对象
     */
    public static void copyProperties(Object source, Object target) {
        BeanUtils.copyProperties(source, target);
    }

    /**
     * 复制属性并返回新对象
     * @param source 源对象
     * @param targetClass 目标类
     * @param <T> 目标类型
     * @return 新对象
     */
    public static <T> T copyProperties(Object source, Class<T> targetClass) {
        try {
            T target = targetClass.newInstance();
            copyProperties(source, target);
            return target;
        } catch (Exception e) {
            throw new RuntimeException("Bean复制失败", e);
        }
    }
}